package cl.utfsm.inf.lp.sem12013.tarea2.logic;

import java.awt.Point;

public class TrainStationEntity extends AbstractStationEntity {
	
	public TrainStationEntity(String name, Point[] p) {
		super(name, p);
	}
	
		
	public void loadTrain(TrainVehicle t){
		String res;
		int count = 0;
		long aux =0;
		long amount;
		for(int i=0; i<t.getResources().size();i++){
			res = t.getResources().get(i);
			do{
				count = 0;
				if(this.entities.get(count).generatesThisResources(res)){
					amount = this.entities.get(count).getGeneratedResourceStock(res);
					aux = t.increaseResourceLoad(res, amount);
					if(aux==0)
						this.entities.get(count).decreaseGeneratedResStock(res, amount);
					else
						this.entities.get(count).decreaseGeneratedResStock(res, aux);
				}
				count++;
			}while(aux==0 && count < this.entities.size());
		}
	}

	public void unloadTrain(TrainVehicle t){
		String res;
		int count = 0;
		long aux =0;
		long amount;
		for(int i=0; i<t.getResources().size();i++){
			res = t.getResources().get(i);
			do{
				count = 0;
				if(this.entities.get(count).requiresThisResource(res)){
					amount = this.entities.get(count).getRequiredResourceStock(res);
					aux = t.decreaseResourceLoad(res, amount);
					if(aux==0)
						this.entities.get(count).increaseRequiredResStock(res, amount);
					else
						this.entities.get(count).increaseRequiredResStock(res, aux);
				}
				count++;
			}while(aux==0 && count < this.entities.size());
		}
	}
}
